package com.zhm.demo.utils;

import org.springframework.util.ObjectUtils;

import java.time.Instant;

/**
 * 时间工具类
 *
 * @author ruoyi
 */
public class DateUtils {


    /**
     * 获取两千年前时间戳
     *
     * @param currentMillis 当前米利斯
     * @return {@link Long}
     */
    public static Long getTimestampByTwoThousandYearsAgo(Long currentMillis) {
        // 获取当前时间的毫秒级时间戳
        if (ObjectUtils.isEmpty(currentMillis)) {
            currentMillis = System.currentTimeMillis();
        }
        // 转换为秒级时间戳
        long currentSeconds = currentMillis / 1000;
        // 获取2000年的秒级时间戳 946684800 seconds is the timestamp for 2000-01-01 00:00:00
        Instant instant = Instant.ofEpochSecond(946684800L);
        long year2000Seconds = instant.getEpochSecond();
        // 计算从2000年到现在的秒数差
        return currentSeconds - year2000Seconds;
    }

    public static Long getTsMsByTwoThousandYearsAgo(Long currentMillis) {
        // 获取当前时间的毫秒级时间戳
        if (ObjectUtils.isEmpty(currentMillis)) {
            currentMillis = System.currentTimeMillis();
        }
        // 转换为秒级时间戳
        // 获取2000年的秒级时间戳 946684800 seconds is the timestamp for 2000-01-01 00:00:00
        Instant instant = Instant.ofEpochSecond(946684800L);
        long year2000Seconds = instant.getEpochSecond();
        // 计算从2000年到现在的秒数差
        return currentMillis - (year2000Seconds * 1000);
    }
    /**
     * 将时间戳恢复到两千年前
     *
     * @param currentMillis 当前米利斯
     * @return {@link Long}
     */
    public static Long restoreTimestampsByTwoThousandYearsAgo(Long currentMillis) {
        long year2000Seconds = Instant.ofEpochSecond(946684800L).getEpochSecond();
        return currentMillis * 1000 + year2000Seconds * 1000;
    }

    public static Long restoreTsMSByTwoThousandYearsAgo(Long currentMillis) {
        long year2000Seconds = Instant.ofEpochSecond(946684800L).getEpochSecond();
        return currentMillis + year2000Seconds * 1000;
    }


}
